package org.androidpush.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.androidpush.R;

import java.util.List;
import java.util.Map;

/**
 * Created by huang on 2017/4/15 0015.
 */
public class MeAdapter extends BaseAdapter {

    private Context context;

    private List<Map<String, Object>> datas;

    private LayoutInflater inflater;

    public MeAdapter(Context context, List<Map<String, Object>> datas) {
        this.context = context;
        this.datas = datas;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vHolder = null;
        String name = (String) datas.get(position).get("name");
        String value = (String) datas.get(position).get("value");
        int img = (int) datas.get(position).get("img");

        if (convertView == null) {
            vHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.xml_me_item, null);
            vHolder.tv_value = (TextView) convertView
                    .findViewById(R.id.id_meitem_value);
            vHolder.tv_name = (TextView) convertView
                    .findViewById(R.id.id_meitem_name);
            vHolder.img_name = (ImageView) convertView.findViewById(R.id.id_meitem_img);
            convertView.setTag(vHolder);
        } else {
            vHolder = (ViewHolder) convertView.getTag();
        }
        vHolder.tv_name.setText(name);
        if (value != null) {
            vHolder.tv_value.setText(value);
        }else {
            vHolder.tv_value.setText("");
        }
        vHolder.img_name.setImageDrawable(convertView.getResources().getDrawable(img));
        return convertView;
    }

    class ViewHolder {
        TextView tv_name;
        TextView tv_value;
        ImageView img_name;
    }
}
